import unittest
import sys,  os
from pylab import *
sys.path.append( os.getcwd() )
from intensbombeocc import *
from pozonoperm import *
from datetime import *
import cProfile

class TestPozoNoPerm(unittest.TestCase):
  def setUp(self):    
    # Custodio Confinado
    #r = 10
    #t = [1,3,6,10,20,40,70,100,200,400,700,1000,2000,3000]
    #ti = []
    #s = [3.4,4.2,4.8,5.08,5.60,6.05,6.40,6.65,7.10,7.50,7.90,8.25,8.70,8.90]
    #Q = 100.0/1000.0*60*60*24.0
    
    # Custodio Semiconfinado
    # r = 10
    # t = [1,3, 5, 6, 7, 10, 20, 30, 60, 80, 100, 200, 400, 700, 1000]
    # ti = []
    # s = [2.03, 2.20, 2.22, 2.25, 2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30]
    # Q = 60*60*60*24/1000

    # Custodio Libre
#    r = 10
#    t = [10, 30, 50, 100, 300, 1000, 10000]
#    ti = []
#    s = [0.024, 0.18, 0.31, 0.52, 1.0, 1.38, 2.30]
#    Q = 2000
#    h0 = 5
#
#    # C-Vignar
#    #ti = [] 
#    #r = 0.15
#    #t = [0.166666667,  0.5,  1,  1.5,  2,  3,  4,  5,  6.5,  8,  9.5,  11,  13,  15,  17,  19,  21, 23,  28,  33,  38,  43,  50.5,  58,  68,  78,  93,  108]
#    #s = [4.34,  6.03, 6.72,  7.21,  7.44, 7.67, 7.9, 8.07,  8.23, 8.38, 8.46, 8.56, 8.64, 8.74, 8.79, 8.84, 8.88, 8.92, 9.01, 9.09, 9.14, 9.19, 9.24, 9.3, 9.34, 9.38, 9.39, 9.42]
#    #Q = 51*24
#    
#    
#    # Internet http://web.usal.es/~javisan/hidro/practicas/confinado/Theis_EXPLICACION.pdf
#    #r = 150
#    #t = [7, 10, 20, 40, 70, 120, 250]
#    #ti = []
#    #s = [1.8, 2.15, 3.0, 3.80, 4.6, 5.25, 6.05]
#    #Q = 20*60*60*24/1000.0
#    
#    for tt in t:
#      ti.append(tt/24.0/60.0)
#    self.iCC = IntEnsBombeoCC('L', Q,  r,  ti,  s, h0 = h0)
    pass

#  def testDistRelativa(self):
#    # make sure the shuffled sequence does not lose any elements
#    v = self.iCC.distRelativa(self.mat)
#    self.assertEqual(v[0],  0.4011111)
#    self.assertEqual(v[1],  0.0606101)
#    self.assertEqual(v[2],  0.0235297)
   
  def testcalcParametrosAcLibre(self):
    # Custodio Libre - Pagina 683
    r = 10
    t = [10, 30, 50, 100, 300, 1000, 10000]
    ti = []
    s = [0.024, 0.18, 0.31, 0.52, 1.0, 1.38, 2.30]
    Q = 2000
    hi = 5
    for tt in t:
      ti.append(tt/24.0/60.0)      
    iCC = IntEnsBombeoCC('L', Q,  r,  ti,  s, h0 = hi) 
    di = datetime.now()
    # cProfile.run('iCC.calcParametros()', '/tmp/AcLibre_iCC.calcParametros.prof')
    # cProfile.run('iCC.calcParametros()')
    iCC.calcParametros() 
    df = datetime.now()
    d = (df-di)
    print "Fin testcalcParametrosAcL %d, %d" %  (d.seconds,  d.microseconds)    
    print "Acuifero Libre"
    print "Caudal (m3/d) = %f" % Q
    print "Ho (m) = %f" % hi
    print "Distancia observacion (m) = %f" % r
    print "(t,s) = (%s,%s)" % (ti, s)
    print "Conductividad (m/d) = %f" % iCC.cond
    print "Transmisividad (m2/d) = %f" % iCC.trans
    print "Almacenamiento (-) = %f" % iCC.almac
    print "(t,scalc) = (%s,%s)" % (ti, iCC.sCalc)    
    print "Fin acuifero Libre"
    print "Datos"
    for cont in range(0, len(t)):
      print "%f|%f" % (t[cont], s[cont])
    clf()
    subplot(211)
    loglog ( iCC.alpha,iCC.vD,'.-y',label='Convergencia' )
    xlabel(r'$S/s(d/m)$')
    ylabel('Distancia relativa de mk')
    title('Convergencia')
    grid(True)
    subplots_adjust(wspace = 0.4, hspace = 0.4)
    subplot(212)
    loglog ( ti,s,'or',label='Observado' )
    loglog ( iCC.ttCalc,iCC.ssCalc,'-g',label='Ajustado' )
    # Como quedo?
    Torigen = 390
    Sorigen = 0.2
    Korigen = Torigen / iCC.h0
    # descAcL(self, tt, Q,  r,  k, S, h0)
    pnp = PozoNoPerm()
    desc = pnp.descAcL(iCC.ttCalc, iCC.q, iCC.r, Korigen, Sorigen, iCC.h0)
    loglog ( iCC.ttCalc,desc,'-y',label='Ajustado con libro' )
    # Como quedo?
#    Torigen = 450
#    Sorigen = 0.4
#    Korigen = Torigen / iCC.h0
#    # descAcL(self, tt, Q,  r,  k, S, h0)
#    pnp = PozoNoPerm()
#    desc = pnp.descAcL(iCC.tCalc, iCC.q, iCC.r, Korigen, Sorigen, iCC.h0)
#    loglog ( iCC.tCalc,desc,'--p',label='Ajustado con libro' )    
    xlabel('t (d)')
    ylabel('s (m)')
    title('Ensayo bombeo acuifero libre')
    grid(True)
    import random
    string = ''
    for i in range(9):
      string += random.choice('abcdefghijklmnopqrstuvwxyz0123456789_-ABCDEFGHIJKMLNOPQRSTUVWXYZ')
    string += ".pdf"
    string = "/tmp/" + string  
    savefig(string,dpi=(1024/8))    

  def testcalcParametrosAcC(self):
    print "********************************************************"
    print "*Inicio testcalcParametrosAcC"    
    # Custodio Confinado
    r = 10
    t = [1,3,6,10,20,40,70,100,200,400,700,1000,2000,3000]
    ti = []
    s = [3.4,4.2,4.8,5.08,5.60,6.05,6.40,6.65,7.10,7.50,7.90,8.25,8.70,8.90]
    Q = 100.0/1000.0*60*60*24.0
    for tt in t:
      ti.append(tt/24.0/60.0)    
    iCC = IntEnsBombeoCC('C', Q,  r,  ti,  s)
    di = datetime.now()
    iCC.calcParametros() 
    print "*Resultados Acuifero Confinado"
    print "*Transmisividad (m2/d) = %f" % iCC.trans
    print "*Almacenamiento (-) = %f" % iCC.almac
    print "*Fin resultados acuifero confinado"
    clf()
    text(0.9, 0.9,'IngeSur', color='r',
    fontsize=10, fontname='Courier',
    horizontalalignment='center',
    verticalalignment='center')# ,transform = ax.transAxes,)
    df = datetime.now()
    d = (df-di)
    print "*Fin testcalcParametrosAcC %d, %d" %  (d.seconds,  d.microseconds)
    #TODO: Ver colocar footer con IngeSur srl       
    subplot(211)
    loglog ( iCC.alpha,iCC.vD,'.-y',label='Convergencia' )
    xlabel(r'$S/T(d/m^2)$')
    ylabel('Distancia relativa de mT')
    title('Convergencia')
    grid(True)
    subplots_adjust(wspace = 0.4, hspace = 0.4)
    subplot(212)
    loglog ( ti,s,'or',label='Observado' )
    loglog ( iCC.ttCalc,iCC.ssCalc,'-g',label='Ajustado' )
    xlabel('t (d)')
    ylabel('s (m)')
    title('Ensayo bombeo acuifero confinado')
    grid(True)
    import random
    string = ''
    for i in range(9):
      string += random.choice('abcdefghijklmnopqrstuvwxyz0123456789_-ABCDEFGHIJKMLNOPQRSTUVWXYZ')
    string += ".pdf"
    string = "/tmp/" + string  
    savefig(string,dpi=(1024/8))
    print "********************************************************\n\n"    

  def testcalcParametrosAcSC(self):
    # Custodio Semiconfinado - Pagina 678
#    r = 10
#    t = [1,3, 5, 6, 7, 10, 20, 30, 60, 80, 100, 200, 400, 700, 1000]
#    ti = []
#    s = [2.03, 2.20, 2.22, 2.25, 2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30,2.30]
#    Q = 60*60*60*24/1000 
    
    r = 0.125
    Q = 432
    t=[2,3,4,5,6,7,8,9,10,15,20,25,30,35,40,50,60,90,135,300,600,680,1225,1295,1355]
    s = [3.32,4.01,4.43,4.58,5.05,5.22,5.49,5.84,5.99,6.85,7.19,7.36,7.48,7.56,7.58,7.74,7.82,7.88,7.94,8.26,8.24,8.34,8.52,8.54,8.54]
    ti = []

    for tt in t:
      ti.append(tt/24.0/60.0)
    iCC = IntEnsBombeoCC('SC', Q,  r,  ti,  s)
    iCC.calcParametros() 
    print
    print "***************************************"
    print "*Acuifero Semiconfinado"
    print "*Caudal (m2/d) = %f" % iCC.q
    print "*Distancia (m) = %f" % iCC.r
    print "*Transmisividad (m2/d) = %f" % iCC.trans
    print "*Almacenamiento (-) = %f" % iCC.almac
    print "*r/B (-) = %s" % iCC.selrdB
    B = float(iCC.r)/float(iCC.selrdB)
    print "*B (m) = %f" % B
    print "*Fin acuifero semiconfinado"
    print "***************************************"
    #print "Fin aDatos"
    #for cont in range(0, len(t)):
    #  print "%f|%f" % (t[cont], s[cont])    
    clf()
    text(0.9, 0.9,'IngeSur', color='r',
    fontsize=10, fontname='Courier',
    horizontalalignment='center',
    verticalalignment='center')# ,transform = ax.transAxes,)
    #TODO: Ver colocar footer con IngeSur srl       
    subplot(211)
    print iCC.rdB
    print 
    print iCC.vD
    loglog ( iCC.rdB,iCC.vD,'.-y',label='Convergencia' )
    xlabel(r'$S/T(d/m^2)$')
    ylabel('Distancia relativa de mT')
    title('Convergencia')
    grid(True)
    subplots_adjust(wspace = 0.4, hspace = 0.4)
    subplot(212)
    loglog ( ti,s,'.r',label='Observado' )
    loglog ( iCC.ttCalc,iCC.ssCalc,'-g',label='Ajustado' )
    xlabel('t (d)')
    ylabel('s (m)')
    title('Ensayo bombeo acuifero semiconfinado')
    grid(True)
    import random
    string = ''
    for i in range(9):
      string += random.choice('abcdefghijklmnopqrstuvwxyz0123456789_-ABCDEFGHIJKMLNOPQRSTUVWXYZ')
    string += ".pdf"
    string = "/tmp/" + string  
    savefig(string,dpi=(1024/8)) 


#  def testcalcParametros(self):
#    self.iCC.calcParametros()
#    #print "Matriz de transmisividades"
#    #print self.iCC.mT
#    #print "Fin de Matriz de transmisividades"
#    #print "Vector distancia relativas"
#    #print self.iCC.vD
#    #print "Fin Vector distancia relativas"    
#    print "Transmisividad"
#    print self.iCC.trans
#    print "Fin Transmisividad"
#    print "Coeficiente almacenamiento"
#    print self.iCC.almac
#    print "Fin Coeficiente almacenamiento" 
#    print "Coeficiente almacenamiento"
#    print self.iCC.selrdB
#    print "Fin Coeficiente almacenamiento"    
#    text(0.9, 0.9,'IngeSur', color='r',
#    fontsize=10, fontname='Courier',
#    horizontalalignment='center',
#    verticalalignment='center')# ,transform = ax.transAxes,)
#    #TODO: Ver colocar footer con IngeSur srl   
#    subplot(211)
#    loglog ( self.iCC.alpha,self.iCC.vD,'.-y',label='Convergencia' )
#    xlabel(r'$S/T(d/m^2)$')
#    ylabel('Distancia relativa de mT')
#    title('Convergencia')
#    grid()
#    subplots_adjust(wspace = 0.4, hspace = 0.4)
#    subplot(212)
#    loglog ( self.iCC.t,self.iCC.s,'or',label='Observado' )
#    loglog ( self.iCC.t,self.iCC.sCalc,'-g',label='Ajustado' )
#    xlabel('t (d)')
#    ylabel('s (m)')
#    title('Ensayo bombeo')
#    grid()
#    import random
#    string = ''
#    for i in range(9):
#      string += random.choice('abcdefghijklmnopqrstuvwxyz0123456789_-ABCDEFGHIJKMLNOPQRSTUVWXYZ')
#    string += ".pdf"
#    string = "/tmp/" + string  
#    savefig(string,dpi=(1024/8))


if __name__ == '__main__':
    unittest.main()
